package com.google.code.rbta;

import com.google.code.rbta.library.FileDownloader;

import android.app.Activity;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Handler.Callback;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.Toast;

/**
 * Start from here.
 * 
 * @author zhaoxinyue
 * 
 */
public class ResumeBrokenTransferAndroid extends Activity implements Callback
{
    private static final String TAG        = ResumeBrokenTransferAndroid.class.getSimpleName();
    private Button              mStart;
    private ProgressBar         mIndictor;

    private Handler             mHandlerFD = new Handler( this );

    /** Called when the activity is first created. */
    @Override
    public void onCreate( Bundle savedInstanceState )
    {
        super.onCreate( savedInstanceState );
        setContentView( R.layout.main );
        mStart = (Button) findViewById( R.id.btn_start );
        mIndictor = (ProgressBar) findViewById( R.id.pb_indictor );
    }

    public void download( View _v )
    {
        FileDownloader.download( this,
                Uri.parse( "http://docs.oracle.com/javase/1.4.2/docs/api/java/io/RandomAccessFile.html" ), mHandlerFD );
    }

    @Override
    public boolean handleMessage( Message _msg )
    {
        switch( _msg.what )
        {
            case FileDownloader.FD_START:
            {
                showDownloadingUI();
            }
            break;

            case FileDownloader.FD_FAILED:
            {
                dismissDownloaingUI();
                Toast.makeText( this, _msg.obj.toString(), Toast.LENGTH_LONG ).show();
            }
            break;
            
            case FileDownloader.FD_DONE:
            {
                dismissDownloaingUI();
            }
            break;

            default:
            {
                Log.w( TAG, "Unknow event." );
            }
            break;
        }
        return true;
    }

    private void dismissDownloaingUI()
    {
        mIndictor.setVisibility( View.GONE );
        mStart.setVisibility( View.VISIBLE );
    }

    private void showDownloadingUI()
    {
        mIndictor.setVisibility( View.VISIBLE );
        mStart.setVisibility( View.GONE );
    }
}
